/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Day09_SlideExamples;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *
 * @author Mark
 */
public class demoMap
{

    public static void main(String args[])
    {
        // ** Make a map, specifying both key and value types 
        HashMap<Integer, String> map = new HashMap<Integer, String>();

        // Map Integers to their words 
        map.put(new Integer(1), "one");
        map.put(new Integer(2), "two");
        map.put(3, "three"); // Let the autoboxing make the Integer 
        map.put(4, "four");

        String s = map.get(new Integer(3)); // returns type String 
        System.out.println("The word for 3 is " + s);

        s = map.get(3); // Same as above, with autoboxing 
        System.out.println("Autoboxing: The word for 3 is " + s);


        // map.put("hi", "there"); // NO does not compile 

        // ** Auto unboxing -- converts between Integer and int 
        Integer intObj = new Integer(7);
        int sum = intObj + 3; // intObj unboxes automatically to an int, sum is 10 
        System.out.println("sum is: " + sum);




        // ** More complex example -- map strings to lists of Integer 
        HashMap<String, List<Integer>> counts = new HashMap<String, List<Integer>>();
        
        //Create a list of even numbers
        List<Integer> evens = new ArrayList<Integer>();
        evens.add(2);
        evens.add(4);
        evens.add(6);
        
        //put the key and values into the map
        counts.put("evens", evens);

        // Get the List<Integer> back out... 
        List<Integer> evens2 = counts.get("evens");
        sum = evens2.get(0) + evens2.get(1); // unboxing here, sum is 6
        
        List<Integer> aa = new ArrayList<Integer>();
        List<Integer> bb = new ArrayList<Integer>();
        
        //This next line works but its not a desirable way to do this. instead use .intValue() to ensure an int is returned
        if (evens.get(0) != evens.get(1))
            System.out.println("True");
        
        System.out.println("The sum is now: " + sum);
        
        //QUIRKY - SHOWS THE DRAWBACK OF !=, works up to -127:127
        aa.add(128);
        bb.add(128);
        System.out.println("QUIRKY: "+ (aa.get(0) == bb.get(0)));
        System.out.println("EXPECTED: "+ (aa.get(0).intValue() == bb.get(0).intValue()));
        
        //SIMPLE STRING TEST FOR == VERSUS .equals();
        String a = "Hello";
        String b = "Hello";
        System.out.println("String a == String b is:" + (a == b));
        System.out.println("String a .equals String b is:" + (a.equals(b)));
        
        String a1 = new String ("Hello");
        String b1 = new String ("Hello");
        String c1 = a1;
        System.out.println("String a1 == String b1 is:" + (a1 == b1));
        System.out.println("String a1 .equals String b1 is:" + (a1.equals(b1)));
        System.out.println("String c1 aliased with String a1:" + (c1 == a1));
        
    }
}
